package com.qk.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.entity.Customer;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.service.CustomerService;
import com.qk.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author SweetHurt
 * @description 针对表【customer(客户表)】的数据库操作Service实现
 * @createDate 2025-10-16 11:31:08
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
        implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public PageResult<CustomerDO> getCustomerByPage(CustomerDTO dto) {
        Page<CustomerDO> page = customerMapper.selectCustomerByPage(new Page<>(dto.getPage(), dto.getPageSize()), dto);
        return PageResult.<CustomerDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getRecords())
                .build();
    }

    @Override
    public boolean addCustomer(Customer customer) {
        if (!ObjectUtil.isAllNotEmpty(customer, customer.getPhone(), customer.getName(), customer.getSubject())) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(LocalDateTime.now());
        return save(customer);
    }

    @Override
    public boolean updateCustomer(Customer customer) {
        if (!ObjectUtil.isAllNotEmpty(customer, customer.getPhone(), customer.getName(), customer.getSubject())) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(null);
        customer.setChannel(null);
        return updateById(customer);
    }
}




